<?php
namespace jy_pingdd_pin;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class order extends \plugin{
	public function openteam($pinggoods,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$params=array(
				'uniacid'=>$_SESSION['uniacid'],
				'goodsid'=>$pinggoods['goodsid'],
				'mid'=>$mid,
				'dayid'=>date('Y-m-d',TIMESTAMP),
				'plugin_name'=>'jy_pingdd_pin',
		);
		$teamid=pdo_getcolumn('jy_weishop_groups_team',$params,'id');
		if($teamid){
			return $teamid;
		}
		$insertdata=array(
				'uniacid'=>$_SESSION['uniacid'],
				'goodsid'=>$pinggoods['goodsid'],
				'mid'=>$mid,
				'dayid'=>date('Y-m-d',TIMESTAMP),
				'plugin_name'=>'jy_pingdd_pin',
				'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_weishop_groups_team',$insertdata);
		$teamid=pdo_insertid();
		return $teamid;
	}
	/*
		获取timeid
	*/
	public function getteamid($pinggoods,$status=0,$mid=0){
		//找别人的团
		$mid=$mid?:$_SESSION['uid'];
		$params=array(
				'uniacid'=>$_SESSION['uniacid'],
				'goodsid'=>$pinggoods['goodsid'],
				'mid <>'=>$mid,
				'status'=>0,
				'dayid'=>date('Y-m-d',TIMESTAMP),
				'plugin_name'=>'jy_pingdd_pin',
		);
		if($status!=10){
			$params['status']=$status;
		}
		$teamid=pdo_getcolumn('jy_weishop_groups_team',$params,'id');
		if(!$teamid){
			$teamid=$this->openteam($pinggoods,$mid);
		}
		return $teamid;
	}
	/*
		自动拼团
	*/
	public function create($pinggoods,$yuyue=array()){		
		$mid=$yuyue['mid']?:$_SESSION['uid'];
		$pingtimes=$this->getpingtimes($pinggoods,$mid);
		if($pinggoods['jiateam']<=$pingtimes){
			return false;
		}
		$item=model('core/member')->detail($mid,array('credit1','credit2','mobile','avatar','nickname'));
		if($pinggoods['price']>$item['credit2']){
					$params=array(
						'pingid'=>$pinggoods['id'],
						'msg'=>'参与拼团失败，余额不足！',
						'status'=>0,
						'mid'=>$mid,
					);
					$is_canyufail=$this->is_canyufail($pinggoods['id'],$mid);
					if(!$is_canyufail){
						$this->model('model')->set_log($params,0);
					}
					return false;
		}
		$teamid=$this->getteamid($pinggoods,0,$mid);
		if(!$teamid){
			return false;
		}
		$goods=pdo_get('jy_weishop_goods',array('id'=>$pinggoods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','type'));
		$goods['total']=1;
		$goods['goodsid']=$pinggoods['goodsid'];
		$goods['price']=$new_order['price'];
		$list=array();
		$list[]=$goods;
		$sendtype=$goods['type']==0?1:0;
		$new_order=array(
			'mid'=>$mid?:$_SESSION['uid'],
			'goodsprice'=>$order['total_price'],
			'remark'=>'预约自动拼团',
			'goods'=>$list,
			'merchid'=>$goods['merchid'],
			'sendtype'=>$sendtype,
		);
		$new_order['price']=$pinggoods['price']>0?$pinggoods['price']:0;
		//拼单部分
		$new_order['addressid']=$yuyue['addressid'];
		$new_order['teamid']=$teamid;
		$new_order['is_team']=1;
		$new_order['plugin_name']='jy_pingdd_pin';
		$order_id=model('jy_weishop/order')->create($new_order);
		model('core/member')->cut_credit2($new_order['mid'],$new_order['price'],'自动拼单','jy_pingdd_pin');
		$params=array(
			'paytype'=>'credit',
			'transid'=>'',
			'status'=>10,
		);
		model('jy_weishop/order')->order_pay($order_id,$params);
		$params=array(
			'pingid'=>$pinggoods['id'],
			'msg'=>'参与拼团成功！',
			'status'=>1,
			'dayid'=>date('Y-m-d',TIMESTAMP),
			'mid'=>$new_order['mid'],
		);
		$this->model('model')->set_log($params,0);
		//拼单部分
		$num=pdo_count('jy_weishop_order',array('plugin_name'=>'jy_pingdd_pin','uniacid'=>$_SESSION['uniacid'],'status'=>10,'teamid'=>$new_order['teamid']));
		$goods=pdo_get('jy_weishop_goods',array('id'=>$pinggoods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		$goods=$this->model('model')->up_price($goods);
		if($goods['teamnum']<=$num){
			$this->model('model')->order_success($new_order['teamid']);
		}else{
			$new_order['goodstitle']=$goods['title'];
			$new_order['orderprice']=$new_order['price'];
			model('jy_weishop/notice')->notice_groupfinish_dingcan($new_order);
		}
		return $order_id;
	}
	/*
		确认拼团次数
	*/
	public function getpingtimes($pinggoods,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'pingid'=>$pinggoods['id'],
			'status'=>1,
			'dayid'=>date('Y-m-d',TIMESTAMP),
			'type'=>0,
		);
		return pdo_count('jy_pingdd_pin_log',$data);
	}
	/*
		参与失败？
	*/
	public function is_canyufail($pingid,$mid=0,$timeid=''){
		$mid=$mid?:$_SESSION['uid'];
		$params=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'pingid'=>$pingid,
			'status'=>0,
			'type'=>0,
			'dayid'=>date('Y-m-d',TIMESTAMP),
		);
		$isping=pdo_get('jy_pingdd_pin_log',$params);
		if($isping['id']){
			return true;
		}else{
			return false;
		}
	}
}